Richard G Baldwin (512) 223-4758, NRG Room 4238, Baldwin@DickBaldwin.com, http://www.austincc.edu/baldwin

INEW 2338 Advanced Java Programming

Assignment 1 for Chapter 3

Revised 10/25/04

The files that you deliver for this assignment must include a controlling class file named Asg03_01.class plus all of the source code files that you compiled to produce your class files.

Click here for general requirements regarding all programming assignments.

Click here to download a zip file containing a sample version of this assignment.  This program was tested using Java SDK 1.4.2 running under WinXP in conjunction with MySQL version 4.0.21-win, and JDBC connector version mysql-connector-java-3.0.15-ga.

In order for you to successfully run this program on your machine, it will probably be necessary for you to have the same version of MySQL and the same version of the JDBC connector (designed for your operating system) installed on your machine.

It will also be necessary for you to have created and populated the database and the table identified below, and to have created the user with the specific password identified below.

Before attempting to write the program required by this assignment, you should study lesson 662 entitled Using JDBC with MySQL, Getting Started.  You will find a link to that lesson at http://www.dickbaldwin.com/tocadv.htm.  You should also study the study guide entitled Java Database Connectivity (JDBC)

In addition, be aware that despite my best efforts to avoid it, I finally had to set my classpath to include the location of the JDBC connector as shown below:

CLASSPATH=...\c:\j2sdk1.4.2\jre\lib\ext\mysql-connector-java-3.0.15-ga-bin.jar

When I installed MySQL, I copied the jar file containing the connector into c:\j2sdk1.4.2\jre\lib\ext in an attempt to avoid having to set the classpath.  Having done that, I was able to run JDBC programs from within JCreator, but was unable to run them directly from a command prompt.  I didn't discover that situation until after I had already published Lesson 662.

Now for the details of this assignment.  Write the Java application described below.

When I grade your assignment, I will have MySQL version 4.0.21 running on my computer with the JDBC connector version mysql-connector-java-3.0.15-ga installed.

This assignment requires you to write a JDBC application that will access and display all of the data in a table named studentTable in a database named StudentDB as a user named studentUser with a password of drowssap on the MySQL database.

I recommend that you begin with the code in Listing 1 and fill in the additional code that is required.  However, that is not a requirement as long as your program behaves correctly with the data in my database.

import java.sql.*;
import java.io.*;

public class Asg03_01{
  public static void main(String args[]){

    try {
      processStudentTable();
    }catch( Exception e ){
      e.printStackTrace();
    }//end catch
  }//end main
  //-------------------------------------------//

  static void processStudentTable(){
    try {
      Class.forName("com.mysql.jdbc.Driver");
      String url =
         "jdbc:mysql://localhost:3306/StudentDB";
     
      //Enter your code here

    }catch( Exception e ) {
      e.printStackTrace();
    }//end catch
  }//end processStudentTable
  //-------------------------------------------//

}//end class Asg03_01

Listing 1

All of the data in the table will be SQL type char(This is not the same as the Java type char.)  However, you won't know how many rows of data are in the table.  You also won't know how many columns are in each row.  (Every row has the same number of columns, and none of them are empty.)  Therefore, you must write your program to accommodate a variable number of rows and a variable number of columns per row.

You must display all of the data in the table in a row-column format with the first character in each column lined up vertically as shown in Listing 2.

Jones   Bob
Smith   Joe
Adams   Tom

Listing 2

Listing 2 shows the correct display format for a table with three rows and two columns per row.  However, the table that I will use to test your program won't have three rows with two columns per row.  The number of rows will be different, and the number of columns per row will also be different.

Therefore, your display format must accommodate the variable number of rows and the variable number of columns per row.  It is absolutely necessary that each row shows the correct number of columns, the first character in each column lines up vertically as shown in Listing 2, and the correct value is displayed at the intersection of each row and each column.

-end-

File:  Asg03_01.htm